package top.mable.payment.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import top.mable.common.annotation.SystemServiceLog;
import top.mable.common.vo.GpTradeInfoDTO;
import top.mable.payment.api.AdjustApi;
import top.mable.payment.api.AdjustApi2;
import top.mable.payment.service.GpPayFeeInfoService;
import top.mable.common.vo.GpPayFeeInfoDTO;
import top.mable.common.vo.Result;


import java.util.List;

@RestController
@RequestMapping("/pay")
public class GpPayFeeInfoController {

    @Autowired
    private GpPayFeeInfoService gpPayFeeInfoService;
    @Autowired
    private AdjustApi adjustApi;

    @Autowired
    private AdjustApi2 adjustApi2;



    @RequestMapping("/getAll")
    public Result getAll(){
        List<GpPayFeeInfoDTO> list = gpPayFeeInfoService.list();
        return Result.success(list);
    }

    @SystemServiceLog
    @RequestMapping("/getOne")
    public Result getOne(@RequestBody GpPayFeeInfoDTO gpPayFeeInfoDTO){
        QueryWrapper wrapper = new QueryWrapper<GpPayFeeInfoDTO>();
        wrapper.eq("id",gpPayFeeInfoDTO.getId());
        GpPayFeeInfoDTO one = gpPayFeeInfoService.getOne(wrapper);
        return Result.success(one);

    }

    @RequestMapping("/getTradeInfo")
    public Result getTradeInfo(@RequestBody GpTradeInfoDTO gpTradeInfoDTO){

        Result<GpTradeInfoDTO> oneTradeInfo = null;
        try {
            oneTradeInfo = adjustApi.getOneTradeInfo(gpTradeInfoDTO);
        } catch (Exception e) {
            throw new RuntimeException("获取adjustApi异常");
        }
        return oneTradeInfo;
    }

    @RequestMapping("/updateTradeInfo")
    public Result updateTradeInfo(@RequestBody GpTradeInfoDTO gpTradeInfoDTO){
        Result result = adjustApi2.updateTradeInfo(gpTradeInfoDTO);

        return result;
    }


    @SystemServiceLog
    @RequestMapping("/updateById")
    //@Transactional(propagation = Propagation.REQUIRED)
    public Result updateById(@RequestBody GpPayFeeInfoDTO gpPayFeeInfoDTO){

        boolean b = gpPayFeeInfoService.updateById(gpPayFeeInfoDTO);

        gpPayFeeInfoService.change(gpPayFeeInfoDTO);
        if(b){
            throw new RuntimeException("故意抛异常");
        }

        if (b){
            return Result.success();
        }else {
            return Result.fail("更新失败");
        }

    }





}
